﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AFanDa.BLL;
using AFanDa.Dto.Base;
using AFanDa.Dto.Reponse;

namespace AFanDa.BLL.BLLService
{
    public class TableTypeService
    {
        private readonly BaseService.BaseTableTypeService _TableTypeService = null;
        private readonly BaseService.BaseTableService _baseTableService = null;
        public TableTypeService()
        {
            _TableTypeService = new BaseService.BaseTableTypeService();
            _baseTableService = new BLL.BaseService.BaseTableService();
        }

        /// <summary>
        /// 添加餐桌类型 （彭思期）
        /// </summary>
        /// <param name="_tableType"></param>
        /// <returns></returns>
        public ApiResult AddTableType(Dto.Request.tableType _tableType)
        {



            _TableTypeService.Add(new Dto.EF.tableType()
            {
                TTid = Common.Config.GUID(),
                TTname = _tableType.name,
                TTnum = 0,
                TTspare = _tableType.spare
            });


            var result = _TableTypeService.SaveChange();

            if (result > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "添加成功",

                };
            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "添加失败",
            };


        }
        /// <summary>
        /// 获取 桌子类型 （无条件） （彭思期）
        /// </summary>
        /// <returns></returns>
        public ApiResult getTableType(Dto.Request.tableType tableType)
        {
            var iq = _TableTypeService.QueryAll(o => o.tables.Count, tableType.isAsc, d => d.TTname.Contains(tableType.key));
            var data = iq.Select(d => new tableTypeResponseDto()
            {
                id = d.TTid,
                name = d.TTname,
                tableCount = d.tables.Count
            }).ToList();

            return new ApiResult()
            {
                code = 0,
                message = "获取桌子类型",
                data = data
            };
        }
        /// <summary>
        /// 添加桌子 （彭思期）
        /// </summary>
        /// <returns></returns>
        public ApiResult AddTable(Dto.Request.tableRequestDto _tableRequestDto)
        {
            var iq = _baseTableService.QueryAll(d => d.Tnumber == _tableRequestDto.number);

            if (!iq.Any())
            {
                _baseTableService.Add(new Dto.EF.tables()
                {
                    Tid = _tableRequestDto.id,
                    Tnumber = _tableRequestDto.number,
                    Ttype = _tableRequestDto.type,
                    Toccupy = 0,
                    Tstatus = 0,
                    Tsumber = _tableRequestDto.sumber,
                    TErWeiMa = _tableRequestDto.ErWeiMa

                });
                var result = _baseTableService.SaveChange();


                if (result > 0)
                {
                    return new ApiResult()
                    {
                        code = ApiCode.ok,
                        message = "添加成功"
                    };
                }

                return new ApiResult()
                {
                    code = ApiCode.fail,
                    message = "添加失败"
                };
            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "桌子号码不能重复，已存在该桌子号码"
            };
        }

        /// <summary>
        /// 获取桌子
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult GetTableById(string id)
        {
            var iq = _baseTableService.QueryAll(d => d.Tid == id);

            if (iq.Any())
            {
                var table = iq.Single();
                var data = new Dto.Reponse.tableResponseDto()
                {
                    id = table.Tid,
                    number = table.Tnumber
                };

                return new ApiResult()
                {
                    code = ApiCode.ok,
                    data = data,
                    message = "pk"
                };
            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = ""
            };
        }

        /// <summary>
        /// 根据条件获取桌子信息 (彭思期)
        /// </summary>
        /// <param name="name">桌子名称</param>
        /// <param name="typeid">类型id</param>
        /// <param name="status">桌子状态</param>
        /// <returns></returns>
        public ApiResult GetTable(string name, string typeid, int status)
        {
            var iq = _baseTableService.QueryAll(o => o.Tnumber, false, d => d.Tnumber.Contains(name));

            if (typeid != "")
            {
                iq = iq.Where(d => d.Ttype == typeid);
            }

            if (status != -1)
            {

                iq = iq.Where(d => d.Tstatus == status);
            }


            var data = iq.Select(d => new tableResponseDto()
            {
                id = d.Tid,
                number = d.Tnumber,
                type = d.Ttype,
                status = d.Tstatus,
                occupy = d.Toccupy,
                sumber = d.Tsumber,
                typename = d.tableType.TTname,
                ErWeiMa = d.TErWeiMa
            }).ToList();


            return new ApiResult()
            {
                code = ApiCode.ok,
                message = "获取桌子信息",
                data = data
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult UpdateTableType(Dto.Request.tableType dto)
        {

            _TableTypeService.Update(new Dto.EF.tableType()
            {
                TTid = dto.id,
                TTname = dto.name,
            });

            var result = _TableTypeService.SaveChange();

            if (result > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "修改成功"
                };
            }

            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "修改失败"
            };


        }

        /// <summary>
        /// 修改桌子
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiResult EditTable(Dto.Request.tableRequestDto dto)
        {
            _baseTableService.Update(new Dto.EF.tables()
            {
                Tid = dto.id,
                Tnumber = dto.number,
                Ttype = dto.type,
                Tstatus = dto.status,
                Tsumber = dto.sumber,
                Toccupy = dto.occupy
            });
            var reuslt = _baseTableService.SaveChange();

            if (reuslt > 0)
            {
                return new ApiResult()
                {
                    code = ApiCode.ok,
                    message = "修改成功"
                };
            }
            return new ApiResult()
            {
                code = ApiCode.fail,
                message = "修改成功"
            };

        }




    }
}
